﻿var str = "";
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var nalsu = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var weekTitle = new Array("일", "월", "화", "수", "목", "금", "토");

var calName = "";
var serviceNo = "";

var obj;

function setProperty(sno, name) {
	calName = name;
	serviceNo = sno;
}

function process() {
	$.ajax({
		url: "/Reservation/reserv/getService.res",
		data: {
			sno: serviceNo,
		},
		dataType: "json",
		success: function(service) {
			var date = new Date(service.rsDate);
			year = date.getFullYear();
			month = date.getMonth() + 1;
			drawCal();
			getReservedList(serviceNo, calName);
		}
	});
}

function process2() {
	$.ajax({
		url: "/Reservation/reserv/getService.res",
		data: {
			sno: serviceNo,
		},
		dataType: "json",
		success: function(service) {
			obj = service;
			
			var date = new Date(service.rsDate);
			
			year = date.getFullYear();
			month = date.getMonth() + 1;
			
			drawCal2();
			getReservedList(serviceNo, calName);
		}
	});
}

function drawCal() {
	var firstDay = new Date(year, month - 1, 1);
	var yoil = firstDay.getDay(); //요일 얻기. 0~6(일~토)
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
		nalsu[1] = 29; //윤년 체크
	}
	makeData(yoil, nalsu[month-1], year, month);
	document.getElementById(calName).innerHTML = str;
}

function drawCal2() {
	var firstDay = new Date(year, month - 1, 1);
	var yoil = firstDay.getDay(); //요일 얻기. 0~6(일~토)
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
		nalsu[1] = 29; //윤년 체크
	}
	 makeSelectableData(yoil, nalsu[month-1], year, month);
	document.getElementById(calName).innerHTML = str;
}

function getReservedList(sno, name) {
	$.ajax({
		url: "/Reservation/reserv/getReserv.res",
		data: {
			sno: sno,
		},
		dataType: "json",
		success: function(list) {
			for(var i in list) {
				var date = new Date(list[i]);
				var year = date.getFullYear() + "";
				var month = (date.getMonth() + 1) + "";
				var day = date.getDate() + "";

				if(($("#" + name + " .year").html() == year) && ($("#" + name + " .month").html() == month)) {
					$("#" + name + " .day" + day).css("color", "red");
				}
			}
		}
	});
}

function getService(sno) {
	$.ajax({
		url: "/Reservation/reserv/getService.res",
		data: {
			sno: sno,
		},
		dataType: "json",
		success: function(service) {
			var date = new Date(service.rsDate);
			year = date.getFullYear();
			month = date.getMonth() + 1;
		}
	});
}

function makeData(yoil, nalsu, year, month) {
	str = ""; 
	str += "<table id='calTable' style='width: 100%; height: 100%;'>"; 
	str += "<tr><th style='text-align: center'><p id='prevMonth' style='cursor: pointer' onclick='monthMinus()'>◀</p></th>"
		+ "<th style='text-align: center' colspan='5'><span class='year' style='font-size: 1.2em; margin-top: 5px;'>" + year 
		+ "</span><span>.&nbsp;</span><span class='month' style='font-size: 1.2em; margin-top: 5px;'>" + month + "</span></th>" 
		+ "<th style='text-align: center'><p id='nextMonth' style='cursor: pointer' onclick='monthPlus()'>▶</p></th>"
		+ "</tr>";
	str += "<tr>";
	for ( var day = 0; day < weekTitle.length; day++) {
		str += "<th style='text-align: center'>" + weekTitle[day] + "</th>";
	}
	str += "</tr>";
	//날 수 채우기
	var no = 1;
	var currentCell = 0;
	//var ju = Math.ceil((nalsu + yoil) / 7); //몇주인지가 나옴. 

	for ( var row = 1; row <= 6; row++) {
		str += "<tr style='text-align: center'>"; //이 아래 7바퀴를 돌림
		for ( var col = 1; col <= 7; col++) {
			if (currentCell < yoil || no > nalsu) {
				str += "<td></td>";	//만약 31일 이 아니면 빈칸
				currentCell++;
			} else {
				str += "<td class='day" + no + "'>" + "<p><strong>"  + no + "</strong></p></td>" ;                	    
				no++;                     
			}
		}
		str += "</tr>";
	}
	str += "</table>";
}
function makeSelectableData(yoil, nalsu, year, month) {
	str = ""; 
	str += "<table id='calTable' style='width: 100%; height: 100%;'>"; 
	str += "<tr><th style='text-align: center'><p id='prevMonth' style='cursor: pointer' onclick='monthMinus2()'>◀</p></th>"
		+ "<th style='text-align: center' colspan='5'><span class='year' style='font-size: 1.2em; margin-top: 5px;'>" + year 
		+ "</span><span>.&nbsp;</span><span class='month' style='font-size: 1.2em; margin-top: 5px;'>" + month + "</span></th>" 
		+ "<th style='text-align: center'><p id='nextMonth' style='cursor: pointer' onclick='monthPlus2()'>▶</p></th>"
		+ "</tr>";
	str += "<tr>";
	for ( var day = 0; day < weekTitle.length; day++) {
		str += "<th style='text-align: center'>" + weekTitle[day] + "</th>";
	}
	str += "</tr>";
	//날 수 채우기
	var no = 1;
	var currentCell = 0;
	//var ju = Math.ceil((nalsu + yoil) / 7); //몇주인지가 나옴. 

	for ( var row = 1; row <= 6; row++) {
		str += "<tr style='text-align: center'>"; //이 아래 7바퀴를 돌림
		for ( var col = 1; col <= 7; col++) {
			if (currentCell < yoil || no > nalsu) {
				str += "<td></td>";	//만약 31일 이 아니면 빈칸
				currentCell++;
			} else {
				str += "<td class='day day" + no + "' style='cursor: pointer'>" + "<p><strong>"  + no + "</strong></p></td>" ;                	    
				no++;                     
			}
		}
		str += "</tr>";
	}
	str += "</table>";
}


function monthMinus() {
	if ((month-1) == 0) {
		year--;
		month = 12;
	}else{
		month--;
	}
	drawCal();
	getReservedList(serviceNo, calName);
}

function monthPlus() {
	if (month > 11) {
		year++;
		month = 1;
	}else{
		month++;
	}  
	drawCal();
	getReservedList(serviceNo, calName);
}


function monthMinus2() {
	if ((month-1) == 0) {
		year--;
		month = 12;
	}else{
		month--;
	}
	drawCal2();
	getReservedList(serviceNo, calName);
}

function monthPlus2() {
	if (month > 11) {
		year++;
		month = 1;
	}else{
		month++;
	}  
	drawCal2();
	getReservedList(serviceNo, calName);
}
